package cn.juque.lucenecandy.core.base;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ClassUtil;
import cn.juque.lucenecandy.core.constants.StrConstant;
import lombok.ToString;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TermQuery;

import java.util.List;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>IdQueryWrapperBuilder</li>
 * </ul>
 * @date 2023-12-26
 **/
@ToString
public class IdsQueryWrapperBuilder<T extends BaseEntity> extends AbstractBuilder<T, IdsQueryWrapperBuilder<T>> {

    public IdsQueryWrapperBuilder(Class<T> indexClass, List<String> ids) {
        super(indexClass);
        if (CollUtil.isNotEmpty(ids)) {
            BooleanQuery.Builder idsQuery = new BooleanQuery.Builder();
            ids.stream()
                    .distinct()
                    .forEach(f ->
                            idsQuery.add(new TermQuery(new Term(StrConstant.D_ID, f)), BooleanClause.Occur.SHOULD));
            super.queryMap.put(idsQuery.build(), BooleanClause.Occur.MUST);
        }
    }

    /**
     * 添加排序规则
     *
     * @param sfunction 字段
     * @param type      类型
     * @param reverse   是否倒序
     * @return M
     */
    @Override
    public IdsQueryWrapperBuilder<T> addSort(Sfunction<T, ?> sfunction, SortField.Type type, Boolean reverse) {
        return super.thisType;
    }

    @Override
    public IdsQueryWrapper build() {
        return new IdsQueryWrapper(ClassUtil.getClassName(super.indexClass, false),
                super.indexInfo, super.queryMap, super.fieldToLoad);
    }
}
